package party.damao.issue.common;

import com.alibaba.fastjson.JSON;

import java.io.Serializable;

/**
 * Rest接口返回的数据基类，包含code（状态码）、message（描述信息）、data（实际的数据）
 *
 */
public class RestObject implements Serializable{
	private static final long serialVersionUID = -5089841239965868787L;
	
	private int code;
	private String message;
	private Object data;
	
	
	//正常的状态码为0，其他都为出错或者异常状态
	public final static int CODE_OK = 0;
	
	//出错状态，统一定为300
	public final static int CODE_ERR = 300;
	
	public RestObject()
	{}
	public RestObject(int state, String message, Object data)
	{
		this.code = state;
		this.message = message;
		this.data = data;
	}
	
	public static RestObject newOK(String message)
	{
		return new RestObject(CODE_OK, message, null);
	}
	
	public static RestObject newOK(String message,Object data)
	{
		return new RestObject(CODE_OK, message, data);
	}

	
	public static RestObject newError(String message)
	{
		return new RestObject(CODE_ERR, message, null);
	}
	
	public static RestObject newError(String message,Object data)
	{
		return new RestObject(CODE_ERR, message, data);
	}
	
	public static RestObject newInstance(int code,String message)
	{
		return new RestObject(code, message, null);
	}
	

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}
	
	@Override
    public String toString()
	{
		//已json字符串返回
		return JSON.toJSONString(this);
	}
}
